//
//  ALPEditorPlayVC.m
//  AlpFM
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPEditorPlayVC.h"
#import "UIImage+Blur.h"
#import "ALPEditorPlayModel.h"
#import "ALPEditorHeadView.h" //头部
#import "ALPEditorPlayCell.h"
#import "ALPPlayViewController.h"
#import "ALPEditorTrackTwoModel.h"


@interface ALPEditorPlayVC ()<UITableViewDataSource,UITableViewDelegate,ALPEditorHeadViewDelegate>
@property (nonatomic, strong) ALPEditorPlayModel *model;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIImageView *myTableImage;
@property (nonatomic, strong) ALPEditorHeadView *headView;
@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UILabel *myLable; //排序动画
@property (nonatomic, assign) NSInteger times; //排序点击次数
@property (nonatomic, strong) UIView *myView;  //每组的头VIEW
@property (nonatomic, strong) UIButton *button1;
@end

@implementation ALPEditorPlayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
        self.times = 2;
    
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(310, 580, 30, 30);
    [self.button1 setImage:[UIImage imageNamed:@"huidaodingbu"] forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(huidaodingbu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button1];
    
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.navigationController.navigationBar.alpha = 0;
    
    [self.myTableView registerClass:[ALPEditorPlayCell class] forCellReuseIdentifier:@"cell"];
    
    self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(80, 300, 200, 80)];
    self.myLable.alpha = 0;
    self.myLable.backgroundColor = [UIColor clearColor];
    self.myLable.text = @"排序";
    self.myLable.enabled = NO;
    self.myLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.myLable];
    
    //背景毛玻璃
    UIView *myTableBroungView = [[UIView alloc]initWithFrame:self.view.frame];
    self.myTableImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [myTableBroungView addSubview:self.myTableImage];
    self.myTableView.backgroundView = myTableBroungView;
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.3;
    [myTableBroungView addSubview:view];
    
    //头部
    self.headView = [[ALPEditorHeadView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
    self.headView.backgroundColor = [UIColor clearColor];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消分割线
    self.myTableView.tableHeaderView = self.headView;
    self.headView.xieyiren = self;
    
    [self getData:999999];

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.myView = [[UIView alloc]init];
    self.myView.backgroundColor = [UIColor colorWithRed:83.0 / 255.0 green:84.0 / 255.0 blue:80.0 / 255.0 alpha:1];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
    NSString *str = [NSString stringWithFormat:@"%ld",self.model.track.totalCount];
    NSString *str3 = [NSString stringWithFormat:@"共%@集",str];
    label.text = str3;
    label.textColor = [UIColor whiteColor];
    [self.myView addSubview:label];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"全部集数" forState:UIControlStateNormal];
    button2.frame = CGRectMake(90, 10, 100, 30);
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(quanbujishu:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:button2];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"选集" forState:UIControlStateNormal];
    button1.frame = CGRectMake(215, 10, 40, 30);
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(xuanji:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:button1];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"排序" forState:UIControlStateNormal];
    button.frame = CGRectMake(300, 10, 40, 30);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(paixu:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:button];
    
    return self.myView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.track.list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ALPEditorPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.model.track.list[indexPath.row];
    return cell;
}
//滚动时触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.myTableView.contentOffset.y < 200) {
        [self.view bringSubviewToFront:self.myTableView];
    }else{
        [self.view bringSubviewToFront:self.button1];
    }
}
//回到顶部{
-(void)huidaodingbu:(UIButton *)btn{
    [self.myTableView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(void)getData:(NSInteger)page{
    NSString *str = [NSString string];
    if (page == 999999) {
       str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%ld/true/1/2000?position=%ld&device=android&title=小编推荐",self.idid,self.indice];
    }else{
        str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%ld/true/%ld/20?position=1&device=android&title=小编推荐",self.idid,page];
    }
    
    [ALPAppTools getWithAFNurl:str block:^(id result) {
        self.model = [[ALPEditorPlayModel alloc]init];
        [self.model setValuesForKeysWithDictionary:result];
        //求出字符串 做毛玻璃
        NSURL *url = [NSURL URLWithString:self.model.albummol.coverOrigin];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        self.myTableImage.image  = [image boxblurImageWithBlur:0.5];
        //标题
        self.headView.model = self.model.albummol;
        
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}









//跳到播放页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];  //取消选中状态
    
    ALPPlayViewController *VC = [ALPPlayViewController sharePlayerViewController];
    ALPEditorTrackTwoModel *mol = [self.model.track.list objectAtIndex:indexPath.row];
    VC.song_ID = mol.trackId;
    [self presentViewController:VC animated:YES completion:^{
        
        
    }];

}
-(void)pop{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//排序
-(void)paixu:(UIButton *)btn{
    
    //动画效果                   持续1.5秒    0秒后执行
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        self.myLable.alpha = 0.5;
        self.myLable.backgroundColor = [UIColor blackColor];
        self.myLable.textColor = [UIColor whiteColor];
        self.myLable.font = [UIFont boldSystemFontOfSize:20];
        self.times ++;
        if (self.times % 2 == 1) {
            self.myLable.text = @"以为您更为倒序...";
        }else{
            self.myLable.text = @"以为您更为正序...";
        }
        
        
        
    } completion:^(BOOL finished) {
        self.myLable.alpha = 0;
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:self.model.track.list];
        NSEnumerator *enmuarr = [array reverseObjectEnumerator];
        id value = nil;
        [self.model.track.list removeAllObjects];
        while (value = [enmuarr nextObject]) {
            [self.model.track.list addObject:value];
        }
        
        [self.myTableView reloadData];
        
    }];
    
    
}

-(void)quanbujishu:(UIButton *)btn{
    [self getData:999999];
}

//选集
-(void)xuanji:(UIButton *)btn{
    static BOOL yy = 0;
    if (yy) {
        [self.view1 removeFromSuperview];
        
    }else{
        UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 120, 667)];
        if (self.jishu / 20 * 40 + 50 <= 667) {
            myScrollView.contentSize = CGSizeMake(0, 667);
            self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 667)];

        } else {
            myScrollView.contentSize = CGSizeMake(0, self.jishu / 20 * 40 + 50);
            self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, self.jishu / 20 * 40 + 50)];

        }
        
        myScrollView.bounces = NO;

        for (int i = 0; i < self.jishu / 20 + 1; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(10, i * 40 + 10, 100, 30);
            button.backgroundColor = [UIColor whiteColor];
            NSString *str = [NSString stringWithFormat:@"%d",i * 20 +1];
            NSString *str1 = [NSString stringWithFormat:@"%d",(i + 1)* 20];
            NSString *str2 = [str stringByAppendingString:@"~"];
            NSString *str3 = [str2 stringByAppendingString:str1];
            [button setTitle:str3 forState:UIControlStateNormal];
            [button addTarget:self action:@selector(jinruxuanji:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.tag = i;
            [self.view1 addSubview:button];
        }
        
        self.view1.backgroundColor = [UIColor blackColor];
        self.view1.alpha = 0.7;
        [myScrollView addSubview:self.view1];
        [self.view addSubview:myScrollView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self.view1 addGestureRecognizer:tap];
    }
    yy = !yy;
}
-(void)tapAction:(UITapGestureRecognizer *)tap{
    [self.view1 removeFromSuperview];
}

-(void)jinruxuanji:(UIButton *)btn{
    [self.view1 removeFromSuperview];
    [self getData:btn.tag + 1];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
